package com.sky.service.impl;/*
@author sun
@date 24/07/15
@project IntelliJ IDEA
*/

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    // 新增分类
    public void addOneCategory(CategoryDTO categoryDTO) {
        Category category = new Category();

        BeanUtils.copyProperties(categoryDTO , category);
        category.setStatus(StatusConstant.DISABLE);

        categoryMapper.addOneCategory(category);
    }

    // 分类分页查询
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        Page<Category> pageQuery = categoryMapper.pageQuery(categoryPageQueryDTO);

        return new PageResult(pageQuery.getTotal(),pageQuery.getResult());
    }

    // 根据id删除分类
    public void delCategoryById(Long id) {

        // TODO 在删除前要判断是否关联 菜品 套餐
        // if 判断 如果有就删除

        categoryMapper.delById(id);
    }

    // 修改分类
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.updateCategory(category);
    }

    // 启用、禁用分类
    public void startOrStop(Integer status, Long id) {
        Category category = new Category().builder().status(status).id(id).build();
        categoryMapper.updateCategory(category);
    }
    // TODO 根据类型查询分类
    public List<Category> queryByType(Integer type) {
        List<Category> list = categoryMapper.queryByType(type);
        return list;
    }
}
